package com.cloudlead.common.persistence;

import java.util.Collection;
import java.util.Map;

import com.cloudlead.common.lang.exception.RequestNotAcceptableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

public class ParamAssert {
	protected static final Logger logger = LoggerFactory.getLogger(ParamAssert.class);
	/**
	 * Assert a boolean expression, throwing {@code RequestNotAcceptableException} if
	 * the test result is {@code false}.
	 * 
	 * <pre class="code">
	 * ParamAssert.isTrue(i &gt; 0, &quot;The value must be greater than zero&quot;);
	 * </pre>
	 * 
	 * @param expression
	 *            a boolean expression
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if expression is {@code false}
	 */
	public static void isTrue(boolean expression, String message,
			String propertyPath, Class<?> rootBean) {
		if (!expression) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.isTrue";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	private static void invalid(String message, String propertyPath,
			Class<?> rootBean, String messageTemplate) {
		InterpolatedMessage interpolatedMessage = new InterpolatedMessage();
		interpolatedMessage.setMessage(message);
		interpolatedMessage.setMessageTemplate(messageTemplate);
		interpolatedMessage.setPropertyPath(propertyPath);
		interpolatedMessage.setRootBeanClass(rootBean.getName());
		logger.debug(interpolatedMessage.toString());
		throw new RequestNotAcceptableException(message);
	}

	/**
	 * Assert a boolean expression, throwing {@code RequestNotAcceptableException} if
	 * the test result is {@code false}.
	 * 
	 * <pre class="code">
	 * ParamAssert.isTrue(i &gt; 0);
	 * </pre>
	 * 
	 * @param expression
	 *            a boolean expression
	 * @throws RequestNotAcceptableException
	 *             if expression is {@code false}
	 */
	public static void isTrue(boolean expression, String propertyPath,
			Class<?> rootBean) {
		isTrue(expression, "[Assertion failed] - this expression must be true",
				propertyPath, rootBean);
	}

	/**
	 * Assert that an object is {@code null} .
	 * 
	 * <pre class="code">
	 * ParamAssert.isNull(value, &quot;The value must be null&quot;);
	 * </pre>
	 * 
	 * @param object
	 *            the object to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if the object is not {@code null}
	 */
	public static void isNull(Object object, String message,
			String propertyPath, Class<?> rootBean) {
		if (object != null) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.isNull";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that an object is {@code null} .
	 * 
	 * <pre class="code">
	 * ParamAssert.isNull(value);
	 * </pre>
	 * 
	 * @param object
	 *            the object to check
	 * @throws RequestNotAcceptableException
	 *             if the object is not {@code null}
	 */
	public static void isNull(Object object, String propertyPath,
			Class<?> rootBean) {
		isNull(object, "[Assertion failed] - the object argument must be null",
				propertyPath, rootBean);
	}

	/**
	 * Assert that an object is not {@code null} .
	 * 
	 * <pre class="code">
	 * ParamAssert.notNull(clazz, &quot;The class must not be null&quot;);
	 * </pre>
	 * 
	 * @param object
	 *            the object to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if the object is {@code null}
	 */
	public static void notNull(Object object, String message,
			String propertyPath, Class<?> rootBean) {
		if (object == null) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.notNull";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that an object is not {@code null} .
	 * 
	 * <pre class="code">
	 * ParamAssert.notNull(clazz);
	 * </pre>
	 * 
	 * @param object
	 *            the object to check
	 * @throws RequestNotAcceptableException
	 *             if the object is {@code null}
	 */
	public static void notNull(Object object, String propertyPath,
			Class<?> rootBean) {
		notNull(object,
				"[Assertion failed] - this argument is required; it must not be null",
				propertyPath, rootBean);
	}

	/**
	 * Assert that the given String is not empty; that is, it must not be
	 * {@code null} and not the empty String.
	 * 
	 * <pre class="code">
	 * ParamAssert.hasLength(name, &quot;Name must not be empty&quot;);
	 * </pre>
	 * 
	 * @param text
	 *            the String to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text, String message,
			String propertyPath, Class<?> rootBean) {
		if (!StringUtils.hasLength(text)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.hasLength";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that the given String is not empty; that is, it must not be
	 * {@code null} and not the empty String.
	 * 
	 * <pre class="code">
	 * ParamAssert.hasLength(name);
	 * </pre>
	 * 
	 * @param text
	 *            the String to check
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text, String propertyPath,
			Class<?> rootBean) {
		hasLength(
				text,
				"[Assertion failed] - this String argument must have length; it must not be null or empty",
				propertyPath, rootBean);
	}

	/**
	 * Assert that the given String has valid text content; that is, it must not
	 * be {@code null} and must contain at least one non-whitespace character.
	 * 
	 * <pre class="code">
	 * ParamAssert.hasText(name, &quot;'name' must not be empty&quot;);
	 * </pre>
	 * 
	 * @param text
	 *            the String to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @see StringUtils#hasText
	 */
	public static void hasText(String text, String message,
			String propertyPath, Class<?> rootBean) {
		if (!StringUtils.hasText(text)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.hasText";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that the given String has valid text content; that is, it must not
	 * be {@code null} and must contain at least one non-whitespace character.
	 * 
	 * <pre class="code">
	 * ParamAssert.hasText(name, &quot;'name' must not be empty&quot;);
	 * </pre>
	 * 
	 * @param text
	 *            the String to check
	 * @see StringUtils#hasText
	 */
	public static void hasText(String text, String propertyPath,
			Class<?> rootBean) {
		hasText(text,
				"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank",
				propertyPath, rootBean);
	}

	/**
	 * Assert that the given text does not contain the given substring.
	 * 
	 * <pre class="code">
	 * ParamAssert.doesNotContain(name, &quot;rod&quot;, &quot;Name must not contain 'rod'&quot;);
	 * </pre>
	 * 
	 * @param textToSearch
	 *            the text to search
	 * @param substring
	 *            the substring to find within the text
	 * @param message
	 *            the exception message to use if the assertion fails
	 */
	public static void doesNotContain(String textToSearch, String substring,
			String message, String propertyPath, Class<?> rootBean) {
		if (StringUtils.hasLength(textToSearch)
				&& StringUtils.hasLength(substring)
				&& textToSearch.contains(substring)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.doesNotContain";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that the given text does not contain the given substring.
	 * 
	 * <pre class="code">
	 * ParamAssert.doesNotContain(name, &quot;rod&quot;);
	 * </pre>
	 * 
	 * @param textToSearch
	 *            the text to search
	 * @param substring
	 *            the substring to find within the text
	 */
	public static void doesNotContain(String textToSearch, String substring,
			String propertyPath, Class<?> rootBean) {
		doesNotContain(textToSearch, substring,
				"[Assertion failed] - this String argument must not contain the substring ["
						+ substring + "]", propertyPath, rootBean);
	}

	/**
	 * Assert that an array has elements; that is, it must not be {@code null}
	 * and must have at least one element.
	 * 
	 * <pre class="code">
	 * ParamAssert.notEmpty(array, &quot;The array must have elements&quot;);
	 * </pre>
	 * 
	 * @param array
	 *            the array to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if the object array is {@code null} or has no elements
	 */
	public static void notEmpty(Object[] array, String message,
			String propertyPath, Class<?> rootBean) {
		if (ObjectUtils.isEmpty(array)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.notEmpty";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that an array has elements; that is, it must not be {@code null}
	 * and must have at least one element.
	 * 
	 * <pre class="code">
	 * ParamAssert.notEmpty(array);
	 * </pre>
	 * 
	 * @param array
	 *            the array to check
	 * @throws RequestNotAcceptableException
	 *             if the object array is {@code null} or has no elements
	 */
	public static void notEmpty(Object[] array, String propertyPath,
			Class<?> rootBean) {
		notEmpty(
				array,
				"[Assertion failed] - this array must not be empty: it must contain at least 1 element",
				propertyPath, rootBean);
	}

	/**
	 * Assert that an array has no null elements. Note: Does not complain if the
	 * array is empty!
	 * 
	 * <pre class="code">
	 * ParamAssert.noNullElements(array, &quot;The array must have non-null elements&quot;);
	 * </pre>
	 * 
	 * @param array
	 *            the array to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if the object array contains a {@code null} element
	 */
	public static void noNullElements(Object[] array, String message,
			String propertyPath, Class<?> rootBean) {
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					String messageTemplate = "com.lecast.paas.persistence.ParamAssert.notNullElements";
					invalid(message, propertyPath, rootBean, messageTemplate);
				}
			}
		}
	}

	/**
	 * Assert that an array has no null elements. Note: Does not complain if the
	 * array is empty!
	 * 
	 * <pre class="code">
	 * ParamAssert.noNullElements(array);
	 * </pre>
	 * 
	 * @param array
	 *            the array to check
	 * @throws RequestNotAcceptableException
	 *             if the object array contains a {@code null} element
	 */
	public static void noNullElements(Object[] array, String propertyPath,
			Class<?> rootBean) {
		noNullElements(
				array,
				"[Assertion failed] - this array must not contain any null elements",
				propertyPath, rootBean);
	}

	/**
	 * Assert that a collection has elements; that is, it must not be
	 * {@code null} and must have at least one element.
	 * 
	 * <pre class="code">
	 * ParamAssert.notEmpty(collection, &quot;Collection must have elements&quot;);
	 * </pre>
	 * 
	 * @param collection
	 *            the collection to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if the collection is {@code null} or has no elements
	 */
	public static void notEmpty(Collection<?> collection, String message,
			String propertyPath, Class<?> rootBean) {
		if (CollectionUtils.isEmpty(collection)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.notEmpty";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that a collection has elements; that is, it must not be
	 * {@code null} and must have at least one element.
	 * 
	 * <pre class="code">
	 * ParamAssert.notEmpty(collection, &quot;Collection must have elements&quot;);
	 * </pre>
	 * 
	 * @param collection
	 *            the collection to check
	 * @throws RequestNotAcceptableException
	 *             if the collection is {@code null} or has no elements
	 */
	public static void notEmpty(Collection<?> collection, String propertyPath,
			Class<?> rootBean) {
		notEmpty(
				collection,
				"[Assertion failed] - this collection must not be empty: it must contain at least 1 element",
				propertyPath, rootBean);
	}

	/**
	 * Assert that a Map has entries; that is, it must not be {@code null} and
	 * must have at least one entry.
	 * 
	 * <pre class="code">
	 * ParamAssert.notEmpty(map, &quot;Map must have entries&quot;);
	 * </pre>
	 * 
	 * @param map
	 *            the map to check
	 * @param message
	 *            the exception message to use if the assertion fails
	 * @throws RequestNotAcceptableException
	 *             if the map is {@code null} or has no entries
	 */
	public static void notEmpty(Map<?, ?> map, String message,
			String propertyPath, Class<?> rootBean) {
		if (CollectionUtils.isEmpty(map)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.notEmpty";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}

	/**
	 * Assert that a Map has entries; that is, it must not be {@code null} and
	 * must have at least one entry.
	 * 
	 * <pre class="code">
	 * ParamAssert.notEmpty(map);
	 * </pre>
	 * 
	 * @param map
	 *            the map to check
	 * @throws RequestNotAcceptableException
	 *             if the map is {@code null} or has no entries
	 */
	public static void notEmpty(Map<?, ?> map, String propertyPath,
			Class<?> rootBean) {
		notEmpty(
				map,
				"[Assertion failed] - this map must not be empty; it must contain at least one entry",
				propertyPath, rootBean);
	}

	/**
	 * Assert that the provided object is an instance of the provided class.
	 * 
	 * <pre class="code">
	 * ParamAssert.instanceOf(Foo.class, foo);
	 * </pre>
	 * 
	 * @param clazz
	 *            the required class
	 * @param obj
	 *            the object to check
	 * @throws RequestNotAcceptableException
	 *             if the object is not an instance of clazz
	 * @see Class#isInstance
	 */
	public static void isInstanceOf(Class<?> clazz, Object obj,
			String propertyPath, Class<?> rootBean) {
		isInstanceOf(clazz, obj, "[Assertion failed] - Object of class ["
				+ (obj != null ? obj.getClass().getName() : "null")
				+ "] must be an instance of " + clazz, propertyPath, rootBean);
	}

	/**
	 * Assert that the provided object is an instance of the provided class.
	 * 
	 * <pre class="code">
	 * ParamAssert.instanceOf(Foo.class, foo);
	 * </pre>
	 * 
	 * @param type
	 *            the type to check against
	 * @param obj
	 *            the object to check
	 * @param message
	 *            a message which will be prepended to the message produced by
	 *            the function itself, and which may be used to provide context.
	 *            It should normally end in a ": " or ". " so that the function
	 *            generate message looks ok when prepended to it.
	 * @throws RequestNotAcceptableException
	 *             if the object is not an instance of clazz
	 * @see Class#isInstance
	 */
	public static void isInstanceOf(Class<?> type, Object obj, String message,
			String propertyPath, Class<?> rootBean) {
		notNull(type, "Type to check against must not be null", propertyPath,
				rootBean);
		if (!type.isInstance(obj)) {
			String messageTemplate = "com.lecast.paas.persistence.ParamAssert.isInstanceOf";
			invalid(message, propertyPath, rootBean, messageTemplate);
		}
	}
}
